package com.e2oto.seller.modules.seller.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.e2oto.seller.R;
import com.e2oto.seller.common.utils.RegexTools;
import com.e2oto.seller.common.utils.ToastUtils;

import java.util.List;

/**
 * Created by ZhangLiQiang on 2017/11/10.
 */

public class PhoneListAdapter {

    private List<String> list;
    private LinearLayout container;
    private Context context;

    public PhoneListAdapter(Context context, List<String> dataList, LinearLayout container) {
        this.list = dataList;
        this.context = context;
        this.container = container;
        notifyDataSetChanged();
    }

    private void appendOnePhone(final int position, String phoneNum) {
        final View view = View.inflate(context, R.layout.item_phone_list, null);
        TextView name = (TextView) view.findViewById(R.id.item_phone_list_name);
        EditText phone = (EditText) view.findViewById(R.id.item_phone_list_number);
        TextView delete = (TextView) view.findViewById(R.id.item_phone_list_delete);
        name.setText("电话" + (position + 1));
        phone.setText(phoneNum);
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                list.remove(position);
                notifyDataSetChanged();
            }
        });
        phone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                list.set(position, s.toString());
            }
        });
        container.addView(view);
    }


    public LinearLayout getContainer() {
        return container;
    }

    public List<String> getList() {
        return list;
    }

    public void notifyDataSetChanged() {
        container.removeAllViews();
        for (int i = 0; i < list.size(); i++) {
            appendOnePhone(i, list.get(i));
        }
    }

    /***
     * 传给服务器的数据
     * @return
     */
    public String getSavedPhoneList() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            sb.append(list.get(i) + ",");
        }
        return sb.toString();
    }

    /**
     * 检查电话号码对不对
     */
    public boolean checkPhoneValid() {
        boolean isValid = true;
        for (int i = 0; i < list.size(); i++) {
            if (!RegexTools.isMobileSimple(list.get(i))) {
                isValid = false;
                ToastUtils.INSTANCE.showToast(context, "电话" + (i + 1) + "格式不正确");
                break;
            }
        }
        return isValid;
    }
}
